package oj.hot100;

public class 二叉搜索树中第k小的元素 {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    public int count = 0;
    public int ret = 0;
    public int kthSmallest(TreeNode root, int k) {
        dfs(root,k);
        return ret;
    }
    //中序遍历
    void dfs(TreeNode root,int k) {
        if(root == null || count == k) {
            return;
        }
        dfs(root.left,k);
        count++;
        if(count == k) {
            ret = root.val;
            return;
        }
        dfs(root.right,k);

        return;
    }
}
